import sys

from ddbms.Server import TrackerServer,SiteServer,Client
from ddbms.StaticSchema import StaticSchema

if sys.version_info[0] >= 3:
    raw_input = input

def printHelp():
    print (
'''
Usage: "tracker"|"site"|"client" <URI> [TrackerURI] [(Mysql)host user pwd db] [InitialSchema]
    if "tracker" is selected, <URI> must be provided for itself
    if "client" is selected, <URI> must be provided for connecting site
    if "site" is selected, you must provide <URI>, <TrackerURI>, <Mysql>
    And if this site is started with database, you can 
    specify [InitialSchema] = test for a builtin test database
''')

if __name__ == '__main__':    
    args = sys.argv[1:]
    if len(args) == 0:
        printHelp()
    if args[0].lower() == "tracker":
        if len(args) < 2:
            printHelp()
        else:
            uri = args[1]
            serv = TrackerServer(uri)
            serv.run()
    elif args[0].lower() == "client":
        if len(args) < 2:
            printHelp()
        else:
            debug = False
            uri = args[1]
            if len(args) >= 3:
                if args[2].lower() == "true":
                    debug = True
            client = Client(uri,debug)
            client.run()
    elif args[0].lower() == "site":
        if len(args) < 7:
            printHelp()
        else:
            uri = args[1]
            trackerUri = args[2]
            mysql = (args[3],args[4],args[5],args[6])
            schema = None
            if len(args) >=8:
                if args[7].lower() == "test":
                    schema = StaticSchema()
            serv = SiteServer(uri,trackerUri,mysql,schema)
            serv.run()
    else:
        printHelp()